package com.gooluke.localcache.controller;

import com.gooluke.localcache.annotation.LocalCache;
import com.gooluke.localcache.annotation.LocalCacheEvict;
import com.gooluke.localcache.dto.UserInfoReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author gooluke
 */
@RestController
@RequestMapping("/cache")
@Slf4j
public class CacheController {

    @RequestMapping("/test1")
    @LocalCache(key = "#name", timeout = 10, timeUnit = TimeUnit.HOURS)
    public String test1(@RequestParam String name, @RequestParam int age) {
        log.info("执行了test1方法,name{},age:{}", name, age);
        return "test1 resp - " + name;
    }

    @RequestMapping("/test2")
    @LocalCache(key = "#userInfoReqDTO.name + '_' + #userInfoReqDTO.age")
    public String test2(@RequestBody UserInfoReqDTO userInfoReqDTO) {
        log.info("执行了test2方法,name{},age:{}", userInfoReqDTO.getName(), userInfoReqDTO.getAge());
        return "test2 resp - " + userInfoReqDTO.getName();
    }

    @RequestMapping("/test3")
    @LocalCache(key = "'test3'", timeout = 10)
    public String test3() {
        log.info("执行了test3方法,过期时间10s");
        return "test2 resp";
    }

    @RequestMapping("/test4")
    @LocalCacheEvict(key = "'test3'")
    public String test4() {
        log.info("执行了test4方法，将test3的缓存清除了");
        return "success";
    }

}
